<template>
  <el-drawer
    title="编辑组织"
    :visible.sync="visible"
    :before-close="handleClose"
    :destroy-on-close="true"
    :wrapperClosable="false"
    :size="this.drawerSize"
  >
    <div class="drawer-content">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="roleName">
          <el-input v-model="form.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色代码" prop="roleCode">
          <el-input v-model="form.roleCode"></el-input>
        </el-form-item>
        <el-form-item label="角色类型" prop="roleType">
          <el-select
            v-model="form.roleType"
            :placeholder="placeholderSelect"
            clearable
            class="full"
          >
            <el-option label="超级管理员" value="0"></el-option>
            <el-option label="系统管理员" value="1"></el-option>
            <el-option label="其他角色" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">更新</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-drawer>
</template>

<script>
import { DRAWER_SIZE, PLEASE_INPUT, PLEASE_SELECT } from "@/util/constant";
import { getRoleDetailApi, updateRoleApi } from "@/api/modules/role.api";
export default {
  data() {
    return {
      visible: false,
      form: {
        roleName: "",
        roleCode: "",
        roleType: "",
        roleId: "",
      },
      placeholderSelect: PLEASE_SELECT,
      drawerSize: DRAWER_SIZE,
      rules: {
        roleName: [{ required: true, message: PLEASE_INPUT, trigger: "blur" }],
        roleCode: [{ required: true, message: PLEASE_INPUT, trigger: "blur" }],
        roleType: [{ required: true, message: PLEASE_SELECT, trigger: "blur" }],
      },
    };
  },
  methods: {
    async init(id) {
      this.visible = true;
      const res = await getRoleDetailApi(id);
      this.form.roleName = res.roleName;
      this.form.roleCode = res.roleCode;
      this.form.roleType = res.roleType.toString();
      this.form.roleId = res.roleId;
    },
    handleClose() {
      this.visible = false;
    },
    async handleAdd() {
      this.form.roleType = this.form.roleType ? Number(this.form.roleType) : "";
      await updateRoleApi(this.form);
      this.visible = false;
      this.$emit("getParentTableData");
    },
  },
};
</script>
